﻿using System;
using System.Collections.Specialized;
using System.Web.Mvc;
using System.Web.Routing;
using Web7.Core.Extensions;
using Web7.FilterProviders;
using Web7.Ioc;
using Web7.RouteUrlModifiers;

namespace Web7.Core.Apps
{
    public abstract class App:IApp
    {
        private readonly RouteCollection _routes;
        private readonly IRouteUrlModifier _routeUrlModifier;
        private readonly string _authenticationAppName;

        protected readonly IDependencyInjector DependencyInjector;

        /// <summary>
        /// Resolver Context
        /// </summary>
        protected readonly IDependencyResolver DependencyResolverContext;

        public App(IDependencyResolver dependencyResolver)
        {
            if (dependencyResolver == null)
                throw new ArgumentNullException("dependencyResolver");

            DependencyResolverContext = dependencyResolver;

            _routes = dependencyResolver.GetService<RouteCollection>();
            _routeUrlModifier = Activator.CreateInstance<RouteUrlModifier>() as IRouteUrlModifier;
        }

        public App()
            : this(DependencyResolver.Current)
        {
        }

        #region MapRoute

        protected void MapRoute(string name, string url)
        {
            _routes.MapRoute(name, url, null, null, null, _routeUrlModifier, AppName);
        }
        protected void MapRoute(string name, string url, object defaults)
        {
            _routes.MapRoute(name, url, defaults, null, null, _routeUrlModifier, AppName);
        }
        protected void MapRoute(string name, string url, string[] namespaces)
        {
            _routes.MapRoute(name, url, null, null, namespaces, _routeUrlModifier, AppName);
        }
        protected void MapRoute(string name, string url, object defaults, object constraints)
        {
            _routes.MapRoute(name, url, defaults, constraints, null, _routeUrlModifier, AppName);
        }
        protected void MapRoute(string name, string url, object defaults, string[] namespaces)
        {
            _routes.MapRoute(name, url, defaults, null, namespaces, _routeUrlModifier, AppName);
        }
        protected void MapRoute(string name, string url, object defaults, object constraints, string[] namespaces)
        {
            _routes.MapRoute(name, url, defaults, constraints, namespaces, _routeUrlModifier, AppName);
        }

        #endregion

        /// <summary>
        /// 模块名称
        /// </summary>
        public abstract string AppName { get; }
        /// <summary>
        /// 认证模块名称,当认证不通过时，将通过认证模块去获取登录页
        /// </summary>
        public virtual string AuthenticationAppName { get { return _authenticationAppName; } }
        /// <summary>
        /// 模块设置
        /// </summary>
        public NameValueCollection Settings { get; set; }
        /// <summary>
        /// 初始化模块
        /// </summary>
        public virtual void Initialize() { }
        /// <summary>
        /// 注册路由配置
        /// </summary>
        public virtual void RegisterRoutes() { }
        /// <summary>
        /// 注册过滤器
        /// </summary>
        /// <param name="filterRegistry"></param>
        /// <param name="globalFilters"></param>
        public virtual void RegisterFilters(FilterRegistryFilterProvider filterRegistry, GlobalFilterCollection globalFilters) { }
        /// <summary>
        /// 注册模型绑定器
        /// </summary>
        /// <param name="modelBinders"></param>
        public virtual void RegisterModelBinders(ModelBinderDictionary modelBinders) { }
        /// <summary>
        /// 卸载模块
        /// </summary>
        public virtual void Unload() { }
    }
}
